package org.test.handler;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.test.dto.JsonMessage;
import org.test.pojo.UserInfo;
import org.test.service.UserInfoService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserInfoHandler {
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("login.do")
    public JsonMessage userLogin(String userAccount, String userPwd, HttpSession session, HttpServletResponse response) throws IOException {
        JsonMessage msg = new JsonMessage();
        UserInfo user = userInfoService.selectByAccAndPwd(userAccount, userPwd);
        UserInfo userInfo = userInfoService.selectStaffInfo(userAccount, userPwd);
        if (user != null) {
            // 账号密码存在
            user.setType(1);
            session.setAttribute("user", user);
            // 启用状态
            msg.setId(0);
            msg.setMsg("登录成功！欢迎您：" + user.getUserName());
            msg.setLocation("back_main");
        } else {
            if (userInfo != null) {
                userInfo.setType(2);
                session.setAttribute("user", userInfo);
                // 启用状态
                msg.setId(0);
                msg.setMsg("登录成功！欢迎您：" + userInfo.getUserName());
                msg.setLocation("back_main");
            } else {
                // 账号密码错误 或  用户不存在
                msg.setId(1);
                msg.setMsg("账号密码错误！");
            }
        }
        return msg;
    }

    @GetMapping("/login/dgut")
    public JsonMessage dgut(String  token) {
        System.out.println(token);
        JsonMessage msg = new JsonMessage();
        msg.setId(0);
        msg.setLocation("login");
        msg.setMsg("莞工认证授权成功！");
        return msg;
    }

    @GetMapping("logout.do")
    public JsonMessage userLogout(HttpSession session) {
        session.invalidate();
        JsonMessage msg = new JsonMessage();
        msg.setId(0);
        msg.setLocation("login");
        msg.setMsg("注销成功！");
        return msg;
    }

    @PostMapping("getUserInfo.do")
    public JsonMessage getUserInfo(HttpSession session) {
        JsonMessage msg = new JsonMessage();
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null) {
            msg.setId(1);
            msg.setMsg("登录过期，请重新登录");
            msg.setLocation("login");
            return msg;
        }
        msg.setId(0);
        msg.getDatas().put("user", user);
        return msg;
    }


    @PostMapping("searchUserInfoById.do")
    public JsonMessage searchUserInfoById(Long userId) {
        JsonMessage msg = new JsonMessage();

        UserInfo userInfo = userInfoService.selectUserInfoById(userId);

        msg.setId(0);
        msg.getDatas().put("userInfo", userInfo);
        return msg;
    }

    @PostMapping("updateUserPwdById.do")
    public JsonMessage updateUserPwdById(String oldPwd, String newPwd, HttpSession session) {
        JsonMessage msg = new JsonMessage();
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null) {
            msg.setId(1);
            msg.setMsg("登录过期，请重新登录");
            msg.setLocation("login");
            return msg;
        }
        if (user.getUserPwd().equals(oldPwd)) {
            boolean result = userInfoService.updateUserPwdById(newPwd, user.getUserId());
            if (result) {
                msg.setId(0);
                msg.setMsg("密码修改成功！请重新登录！");
                msg.setLocation("login");
                return msg;
            }
        } else {
            msg.setId(1);
            msg.setMsg("原密码输入错误！");
            return msg;
        }

        msg.setId(1);
        msg.setMsg("密码修改失败！");
        return msg;
    }

    public void sss() throws IOException {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            /**
             * 创建HttpClient对象
             */
            client = HttpClients.createDefault();
            /**
             * 创建URIBuilder
             */
            URIBuilder uriBuilder = new URIBuilder("https://cas.dgut.edu.cn?appid=javaee");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("admin","admin"));
            nvps.add(new BasicNameValuePair("pwd","123456"));
            /**
             * 创建HttpGet
             */
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            /**
             * 设置请求头部编码
             */
            httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
            /**
             * 设置返回编码
             */
            httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
            /**
             * 请求服务
             */
            response = client.execute(httpGet);
            /**
             * 获取响应吗
             */
            int statusCode = response.getStatusLine().getStatusCode();

        } catch (Exception e) {

        } finally {
            response.close();
            client.close();
        }
    }
}

